import axios from 'axios';
import Qs from 'qs';
import {Loading, Message, MessageBox} from 'element-ui';
import {delCookie, getCookie, getStore, removeStore, setCookie, setStore} from "../utils/util";


axios.defaults.timeout = 1800000;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';
axios.defaults.withCredentials = true;

axios.interceptors.request.use(
  config => {
    const token = getCookie('token');
    config.headers.Token = token;
    return config
    },error => {
      return Promise.reject(error);
    }
  )

axios.interceptors.response.use(
  response => {
    console.log("token", response.headers.token)
    // console.log("请求地址", response.config.url)
    if (response.config.url == "/streetdance/user/login") {
      if (response.data.status == 200) {
        console.log("token:",response.headers.token)
        setCookie("token", response.headers.token, 0.5)
        setStore("userInfo", response.data.data)
      }
    }else {
      if (response.headers.token) {
        setCookie("token", response.headers.token, 0.5)
      }else {
        delCookie("token")
        removeStore("userInfo")
      }
    }
    return response
  },
  //接口错误状态处理，也就是说无响应时的处理
  error => {
    //拦截响应，做统一处理
    let errMsg = '';
    if (error.response.status || error.response.data.status) {
      switch (error.response.status) {
        case 400:
          errMsg = '请求参数错误';
          break;

        case 401:
          errMsg = '登录状态失效，请重新登录';
          MessageBox(errMsg, '身份失效', {
            confirmButtonText: '确定',
          }).then(() => {

            location.reload()
          }).catch(() => {

            location.reload()
          });
          return Promise.reject(error.response);

        case 403:
          errMsg = '拒绝访问';
          break;

        case 408:
          errMsg = '请求超时';
          break;

        case 500:
          errMsg = '服务器内部错误';
          break;

        case 501:
          errMsg = '服务未实现';
          break;

        case 502:
          errMsg = '网关错误';
          break;

        case 503:
          errMsg = '服务不可用';
          break;

        case 504:
          errMsg = '网关超时';
          break;

        case 505:
          errMsg = 'HTTP版本不受支持';
          break;

        default:
          errMsg = error.response.data.message;
          break;
      }
    } else {
      if (error.statusText === 'ECONNABORTED') {
        errMsg = '请求超时';
      } else {
        errMsg = error.response.data.message;
      }
    }
    Message.error(errMsg);
    return Promise.reject(error.response); // 返回接口返回的错误信息
  }
)

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function Get(url, params){
  return new Promise((resolve, reject) =>{
    axios.get(url, {
      params: params
    }).then(res => {
      resolve(res.data);
    }).catch(err =>{
      reject(err.data)
    })
  })
}

/**
 * post方法，对应post请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function Post(url, params) {
  return new Promise((resolve, reject) => {
    axios.post(url, Qs.stringify(params))
      .then(res => {
        resolve(res.data);
      })
      .catch(err =>{
        reject(err.data)
      })
  });
}
